/**
 * 
 */
package com.nari.baseinfo.action;

import java.util.List;

import org.apache.struts2.json.JSONUtil;
import org.apache.struts2.json.annotations.JSON;

import com.nari.baseinfo.model.IBuildingUnit;
import com.nari.baseinfo.service.IBuildingUnitService;
import com.nari.coreframe.action.struts2.JSONActionSupport;
import com.nari.platform.model.ResponseMsg;
import com.opensymphony.xwork2.Action;

/**
 * 
 * @author Xiaopeng
 * @date 2013-8-1
 */ 
public class BuildingUnitAction extends JSONActionSupport {
	private static final long serialVersionUID = 235765515145155060L;
	private IBuildingUnitService iBuildingUnitService;
	//楼栋信息查询
	private String buildingUID; //楼栋唯一标识
	private String unitName;
	private List<IBuildingUnit> buildingUnitList;
	//楼栋单元信息管理
	private String buildingUnitUID;
	private IBuildingUnit buildingUnit;
	
	/**
	 * 通过楼栋唯一标识查询单元列表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-8
	 */
	public String queryUnitByBuildingUID() throws Exception {
		this.buildingUnitList = iBuildingUnitService.queryUintListByBuildingUID(buildingUID, unitName);
		return "unitList";
	}
	
	/**
	 * 楼栋单元信息管理
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-8
	 */
	public String queryBuildingUnitForManage() throws Exception {
		if(this.buildingUnitUID!=null && !"".equals(buildingUnitUID)) {
			this.buildingUnit = this.iBuildingUnitService.queryEntityById(buildingUnitUID);
		}
		return "unitManage";
	}
	
	/**
	 * 新增或修改楼栋单元信息
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-12
	 */
	public String saveOrUpdateBuildingUnit() throws Exception {
		int repeatCounter = iBuildingUnitService.countByUnitName(buildingUnit.getUnitName(), 
				buildingUnit.getBuildingID(), buildingUnit.getId());
		if(repeatCounter > 0) {
			this.rmsg = ResponseMsg.getInstance(false, "重复的单元名称【"+buildingUnit.getUnitName()+"】，楼栋单元保存失败！");
		} else {
			if(this.buildingUnit.getId()!=null && !"".equals(buildingUnit.getId())) {
				this.iBuildingUnitService.updateEntity(buildingUnit, buildingUnit.getId());
			} else {
				this.iBuildingUnitService.saveEntity(buildingUnit);
			}
			this.rmsg = ResponseMsg.getInstance(true, "");
		}
		
		this.writeJsonStrToResponse(JSONUtil.serialize(rmsg));
		return Action.SUCCESS;
	}
	
	/**
	 * 通过唯一标识删除楼栋单元信息
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-12
	 */
	public String deleteBuildingUnitByUID() throws Exception {
		this.rmsg = this.iBuildingUnitService.deleteCascadeByUnitUID(buildingUnitUID);
		this.writeJsonStrToResponse(JSONUtil.serialize(rmsg));
		return Action.SUCCESS;
	}
	
	/**
	 * 通过所属楼栋，查询对应的楼栋单元列表
	 * 
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-14
	 */
	public String queryBuildingUnitsByBuildingUID() throws Exception {
		this.buildingUnitList = this.iBuildingUnitService.queryUintListByBuildingUID(buildingUID, null);
		this.writeJsonStrToResponse(JSONUtil.serialize(this.buildingUnitList));
		return Action.SUCCESS;
	}
	
	/**
	 * @return the iBuildingUnitService
	 */
	@JSON(serialize=false)
	public IBuildingUnitService getiBuildingUnitService() {
		return iBuildingUnitService;
	}
	
	/**
	 * @param iBuildingUnitService the iBuildingUnitService to set
	 */
	public void setiBuildingUnitService(IBuildingUnitService iBuildingUnitService) {
		this.iBuildingUnitService = iBuildingUnitService;
	}

	/**
	 * @return the buildingUID
	 */
	public String getBuildingUID() {
		return buildingUID;
	}

	/**
	 * @param buildingUID the buildingUID to set
	 */
	public void setBuildingUID(String buildingUID) {
		this.buildingUID = buildingUID;
	}

	/**
	 * @return the unitName
	 */
	public String getUnitName() {
		return unitName;
	}

	/**
	 * @param unitName the unitName to set
	 */
	public void setUnitName(String unitName) {
		this.unitName = unitName;
	}

	/**
	 * @return the buildingUnitList
	 */
	public List<IBuildingUnit> getBuildingUnitList() {
		return buildingUnitList;
	}

	/**
	 * @param buildingUnitList the buildingUnitList to set
	 */
	public void setBuildingUnitList(List<IBuildingUnit> buildingUnitList) {
		this.buildingUnitList = buildingUnitList;
	}

	/**
	 * @return the buildingUnitUID
	 */
	public String getBuildingUnitUID() {
		return buildingUnitUID;
	}

	/**
	 * @param buildingUnitUID the buildingUnitUID to set
	 */
	public void setBuildingUnitUID(String buildingUnitUID) {
		this.buildingUnitUID = buildingUnitUID;
	}

	/**
	 * @return the buildingUnit
	 */
	public IBuildingUnit getBuildingUnit() {
		return buildingUnit;
	}

	/**
	 * @param buildingUnit the buildingUnit to set
	 */
	public void setBuildingUnit(IBuildingUnit buildingUnit) {
		this.buildingUnit = buildingUnit;
	}
	
}
